//
//  DateUtils.m
//  OCLearning
//
//  Created by Caldremch on 2018/5/26.
//  Copyright © 2018年 Caldremch. All rights reserved.
//

#import "DateUtils.h"


@implementation DateUtils

//获取当前系统时间戳
+(long) getTimeStamp{
    long time;
    //还有一个 NSData, 别搞混了
    NSDate *d = [NSDate date];
    time = (long) [d timeIntervalSince1970];
    return time;
}

+ (NSDate *)changeTimeStamp2Time:(NSString *)spString{
    
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:[spString intValue]];
    return d;
    
}

//将时间戳转换成NSDate,加上时区偏移
+(NSDate*)zoneChange:(NSString*)spString{
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[spString intValue]];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:confromTimesp];
    NSDate *localeDate = [confromTimesp  dateByAddingTimeInterval: interval];
    return localeDate;
}


+ (long)timeDifferent:(NSDate *)date{
    
    NSDate *localDate = [NSDate date];
    long different = fabs([localDate timeIntervalSinceDate:date]);
    return different;
    
}


+ (NSString *)nsDate2String:(NSDate *)date{
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString* string=[dateFormat stringFromDate:date];
    return string;
}

//将yyyy-MM-dd HH:mm:ss格式时间转换成时间戳
+(long)changeTimeToTimeSp:(NSString *)timeStr{
    long time;
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *fromdate=[format dateFromString:timeStr];
    time= (long)[fromdate timeIntervalSince1970];
    [format release];
    return time;
}
//获取当前系统的yyyy-MM-dd HH:mm:ss格式时间
+(NSString *)getTime{
    NSDate *fromdate=[NSDate date];
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString* string=[dateFormat stringFromDate:fromdate];
    return string;
}

@end
